<?php
namespace app\admin\controller;
/**
 * #Vip管理#{管理可系统Vip发放等}
 */
class Vip extends Common{
    /**
     * #vip列表#
     * @return [type] [description]
     */
    function index(){
        $module_id=input('module_id/d',0);
        $artmodules=model('artmodule')->readAll([],0,['field'=>'id,title']);
        $this->assign('artmodules',$artmodules);

        $map=[];
        if($module_id>0){
            $map=['module_id'=>$module_id-1];
        }
        
        $list=model($this->table)->readAll($map,true,['order'=>'sort','with'=>'module']);
        $this->assign('list',$list);
        $this->assign('module_id',$module_id);

        return view();
    }
    /**
     * #VIP新增、编辑#
     * @param integer $id [description]
     */
    function add($id=0){
        if(request()->isAjax()||request()->isPost()){
            $data=[
                'id'=>$id,
                'module_id'=>input('module_id/d',1),
                'name'=>input('name/s',''),
                'status'=>input('status/d',1),
                'remark'=>input('remark/s',''),
                'label'=>input('label/s',''),
                'raw_money'=>input('raw_money/d',0),
                'money'=>input('money/d',0),
                'ratio'=>input('ratio/d',0),
                'y'=>input('y/d',0),
                'm'=>input('m/d',0),
                'd'=>input('d/d',0),
                'level'=>input('level/d',1),
            ];
            $validate=$this->validate($data,[
                'name|VIP名称'=>'require',
                'module_id|模块'=>'require',
                'money|价格'=>'require|min:1',
            ]);
            if(true!==$validate){
                $this->error($validate);
            }
            if($data['y']<=0&&$data['m']<=0&&$data['d']<=0){
                $this->error('请设置VIP时长');
            }
            if($data['raw_money']>0&&$data['raw_money']<$data['money']){
                $this->error('暂不允许原价小于现价');
            }
            if($id>0){
                $has=model($this->table)->where(['id'=>$id])->count();
                if(!$has){
                    $this->error('该条记录不存在或已删除');
                }
                
            }
            $hasModule=model('artmodule')->where(['id'=>$data['module_id']])->count();
            if(!$hasModule){
                $this->error('模块不存在');
            }
            $o=false;
            if($id>0){
                $data['id']=$id;
                $o=model($this->table)->edit($data);
            }else{
                $o=model($this->table)->add($data);
            }
            if($o){
                $this->success(lang('Successful operation'));
                die;
            }
            $this->error(lang('Failed operation'));
        }
    }
    /**
     * #vip删除#
     * @param  integer $id [description]
     * @return [type]      [description]
     */
    function vipDel($id=0){
        if(request()->isAjax()||request()->isPost()){
            $ids=input('ids/a',[]); // 多项
            $map=[];
            if(empty($ids)){
                $map=['id'=>$id];
            }else{
                $map=['id'=>['id','in',$ids]];
            }
            $rs=model($this->table)->del($map);
            if($rs){
                $this->success(lang('Successful operation'));
                die;
            }
            $this->error(lang('Failed operation'));
        }
    }
    /**
     * #VIP启用、禁用#
     * @param  integer $id [description]
     * @return [type]      [description]
     */
    function close($id=0){
        if(request()->isAjax()||request()->isPost()){
            $ids=input('ids/a',[]); // 多项
            $rs=$this->_close('vip',$id,$ids);
            if($rs['success']){
                $this->success($rs['msg']);
                die;
            }
            $this->error($rs['msg']);
        }
    }
    
}